package com.situ.crm.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.crm.VO.LayUISelectMVO;
import com.situ.crm.common.ServerResponse;
import com.situ.crm.entity.Permission;
import com.situ.crm.mapper.PermissionMapper;
import com.situ.crm.service.IPermissionService;

@Service
public class PermissionServiceImpl implements IPermissionService {

	@Autowired
	private PermissionMapper permissionMapper;

	@Override
	public ServerResponse pageList(Integer page, Integer limit) {
		// 1、使用PageHelper插件设置分页
		PageHelper.startPage(page, limit);
		// 2、执行查询
		List<Permission> list = permissionMapper.pageList();
		// 3、使用PageInfo对结果进行包装
		PageInfo pageInfo = new PageInfo(list);
		Integer count = (int) pageInfo.getTotal();// 得到总数量

		return ServerResponse.createSuccess("查询成功", count, list);
	}

	@Override
	public ServerResponse selectAllPermissions() {
		List<Permission> permissions = permissionMapper.pagelist(new Permission());
		// selectM需要的结构：{"id":12,"name":"长者","status":0}，
		// 所以要转换一下
		List<LayUISelectMVO> list = new ArrayList<>();
		for (Permission permission : permissions) {
			LayUISelectMVO layUISelectMVO = new LayUISelectMVO();
			layUISelectMVO.setId(permission.getId());
			layUISelectMVO.setName(permission.getName());
			layUISelectMVO.setStatus(1);
			list.add(layUISelectMVO);
		}

		return ServerResponse.createSuccess("查找成功", list);
	}

	@Override
	public ServerResponse insert(Permission record) {
		Integer count = permissionMapper.insertSelective(record);
		if (count == 1) {
			return ServerResponse.createSuccess("添加成功");
		}else{
			return ServerResponse.createSuccess("添加失败");
		}
	}

}
